package main

import (
	"context"
	"io"
	"log"

	"gitee.com/hlwqds/go-project/grpc-demo/client/cmd"
	pb "gitee.com/hlwqds/go-project/grpc-demo/proto"
	"google.golang.org/grpc"
)

func main() {
	cmd.Execute()
	conn, _ := grpc.Dial(":"+cmd.Port, grpc.WithInsecure())
	defer conn.Close()
	client := pb.NewGreeterClient(conn)
	_ = SayHello(client)
	_ = SayList(client)
}

func SayHello(client pb.GreeterClient) error {
	resp, _ := client.SayHello(context.Background(), &pb.HelloRequest{Name: "hlwqds"})
	log.Printf("client.SayHello resp: %s\n", resp.Message)
	return nil
}

func SayList(client pb.GreeterClient) error {
	stream, _ := client.SayList(context.Background(), &pb.HelloRequest{Name: "hlwqds"})
	for {
		resp, err := stream.Recv()
		if err == io.EOF {
			return nil
		}
		if err != nil {
			return err
		}
		log.Printf("resp: %v", resp)
	}
}
